public class MyViewFive extends View {
private static final String TAG = "MyViewFive";
private int count = 6;
private float angle = (float) (Math.PI * 2 / count);
private float radius;
private int centerX;
private int centerY;
private String[] titles = {"a", "b", "c", "d", "e", "f"};
private double[] data = {100, 60, 90, 30, 100, 50, 10, 20};
private float maxValue = 100;
private Paint mainPaint;
private Paint valuePaint;
private Paint textPaint;
public MyViewFive(Context context) {
super(context);
init();
}
public MyViewFive(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
radius = Math.min(w, h) / 2 * 0.9f;
centerX = w / 2;
centerY = h / 2;
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPolygon(canvas);
drawLines(canvas);
drawText(canvas);
drawValue(canvas);
}
private void init(){
mainPaint=new Paint();
mainPaint.setColor(Color.BLACK);
mainPaint.setStyle(Paint.Style.STROKE);
mainPaint.setStrokeWidth(3f);
textPaint=new Paint();
textPaint.setColor(Color.BLUE);
textPaint.setTextSize(50f);
valuePaint=new Paint();
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
valuePaint.setColor(Color.BLUE);
}
private void drawPolygon(Canvas canvas){
Path path = new Path();
float r = radius / (count - 1);
for (int i = 1; i < count; i++) {
float curR = r * i;
path.reset();
for (int j = 0; j < count; j++) {
if (j == 0) {
path.moveTo(centerX + curR, centerY);
} else {
float x = (float) (centerX + curR * Math.cos(angle * j));
float y = (float) (centerY + curR * Math.sin(angle * j));
path.lineTo(x, y);
}
}
path.close();
canvas.drawPath(path, mainPaint);
}
}
private void drawLines(Canvas canvas){
Path path=new Path();
for (int i = 0; i < count; i++) {
path.reset();
path.moveTo(centerX,centerY);
float x= (float) (centerX+radius*Math.cos(angle*i));
float y= (float) (centerY+radius*Math.sin(angle*i));
path.lineTo(x,y);
canvas.drawPath(path,mainPaint);
}
}
private void drawText(Canvas canvas) {
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
float fontHeight = fontMetrics.descent - fontMetrics.ascent;
for (int i = 0; i < count; i++) {
float x = (float) (centerX + (radius + fontHeight / 2) * Math.cos(angle * i));
float y = (float) (centerY + (radius + fontHeight / 2) * Math.sin(angle * i));
canvas.drawText(titles[i], x , y, textPaint);
}
}
private void drawValue(Canvas canvas){
Path path=new Path();
valuePaint.setAlpha(245);
for (int i = 0; i < count; i++) {
double percentage=data[i]/maxValue;
float x= (float) (centerX+radius*Math.cos(angle*i)*percentage);
float y= (float) (centerY+radius*Math.sin(angle*i)*percentage);
if (i==0){
path.moveTo(x,y);
}else {
path.lineTo(x,y);
}
canvas.drawCircle(x,y,10,valuePaint);
}
valuePaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,valuePaint);
valuePaint.setAlpha(170);
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawPath(path,valuePaint);
}
}
public class MyViewFive extends View {
private static final String TAG = "MyViewFive";
private int count = 6;
private float angle = (float) (Math.PI * 2 / count);
private float radius;
private int centerX;
private int centerY;
private String[] titles = {"a", "b", "c", "d", "e", "f"};
private double[] data = {100, 60, 90, 30, 100, 50, 10, 20};
private float maxValue = 100;
private Paint mainPaint;
private Paint valuePaint;
private Paint textPaint;
public MyViewFive(Context context) {
super(context);
init();
}
public MyViewFive(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
radius = Math.min(w, h) / 2 * 0.9f;
centerX = w / 2;
centerY = h / 2;
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawPolygon(canvas);
drawLines(canvas);
drawText(canvas);
drawValue(canvas);
}
private void init(){
mainPaint=new Paint();
mainPaint.setColor(Color.BLACK);
mainPaint.setStyle(Paint.Style.STROKE);
mainPaint.setStrokeWidth(3f);
textPaint=new Paint();
textPaint.setColor(Color.BLUE);
textPaint.setTextSize(50f);
valuePaint=new Paint();
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
valuePaint.setColor(Color.BLUE);
}
private void drawPolygon(Canvas canvas){
Path path = new Path();
float r = radius / (count - 1);
for (int i = 1; i < count; i++) {
float curR = r * i;
path.reset();
for (int j = 0; j < count; j++) {
if (j == 0) {
path.moveTo(centerX + curR, centerY);
} else {
float x = (float) (centerX + curR * Math.cos(angle * j));
float y = (float) (centerY + curR * Math.sin(angle * j));
path.lineTo(x, y);
}
}
path.close();
canvas.drawPath(path, mainPaint);
}
}
private void drawLines(Canvas canvas){
Path path=new Path();
for (int i = 0; i < count; i++) {
path.reset();
path.moveTo(centerX,centerY);
float x= (float) (centerX+radius*Math.cos(angle*i));
float y= (float) (centerY+radius*Math.sin(angle*i));
path.lineTo(x,y);
canvas.drawPath(path,mainPaint);
}
}
private void drawText(Canvas canvas) {
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
float fontHeight = fontMetrics.descent - fontMetrics.ascent;
for (int i = 0; i < count; i++) {
float x = (float) (centerX + (radius + fontHeight / 2) * Math.cos(angle * i));
float y = (float) (centerY + (radius + fontHeight / 2) * Math.sin(angle * i));
canvas.drawText(titles[i], x , y, textPaint);
}
}
private void drawValue(Canvas canvas){
Path path=new Path();
valuePaint.setAlpha(245);
for (int i = 0; i < count; i++) {
double percentage=data[i]/maxValue;
float x= (float) (centerX+radius*Math.cos(angle*i)*percentage);
float y= (float) (centerY+radius*Math.sin(angle*i)*percentage);
if (i==0){
path.moveTo(x,y);
}else {
path.lineTo(x,y);
}
canvas.drawCircle(x,y,10,valuePaint);
}
valuePaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path,valuePaint);
valuePaint.setAlpha(170);
valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawPath(path,valuePaint);
}
}